package com.lanou3g.k12.service.impl;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou3g.k12.entity.CourseCategory;
import com.lanou3g.k12.mapper.CourseCategoryMapper;
import com.lanou3g.k12.service.CourseCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class CourseCategoryServiceImpl implements CourseCategoryService {
    @Autowired
    private CourseCategoryMapper courseCategoryMapper;
    @Override
    public List<CourseCategory> search(String searchText) {
        List<CourseCategory> byKeyword = courseCategoryMapper.findByKeyword("%" + searchText + "%");
        return byKeyword;
    }

    @Override
    public List<CourseCategory> findAll() {
        List<CourseCategory> all = courseCategoryMapper.findAll();
        return all;
    }

    @Override
    public void delete(Long[] ids) {
        courseCategoryMapper.deleteByIds(ids);
    }

    @Override
    public void insert(CourseCategory courseCategory) {
        if (true) {
            SimpleDateFormat sF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sF.format(new Date());
            Date parse = null;
            try {
                //字符串格式时间转换
                parse = sF.parse(format);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            courseCategory.setCreateTime(parse);
            courseCategory.setUpdateTime(parse);
        }
        courseCategoryMapper.insert(courseCategory);
    }

    @Override
    public CourseCategory selectById(Long id) {
        CourseCategory courseCategory = courseCategoryMapper.selectById(id);
        return courseCategory;
    }

    @Override
    public void update(Long id,CourseCategory courseCategory) {
        if (true) {
            SimpleDateFormat sF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sF.format(new Date());
            Date parse = null;
            try {
                //字符串格式时间转换
                parse = sF.parse(format);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            courseCategory.setUpdateTime(parse);
        }
        courseCategoryMapper.updateById(id,courseCategory);
    }

    @Override
    public void deleteById(Long id) {
        courseCategoryMapper.deleteById(id);
    }
}
